<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="mobile"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 1.875rem"
        @click.native.prevent="handleLogin"
      >登 录</el-button>

      <!-- <div class="tips">
        <span style="margin-right: 1.25rem">mobile: admin</span>
        <span> password: any</span>
      </div> -->
    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
// import { loginApi, getInfo } from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      userInfo: {},
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入合法手机号码',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword },
          {
            pattern: /^\w+$/,
            message: '密码只能是英文、数字和下划线组成',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 发送请求
          // loginApi(this.loginForm).then(async(res) => {
          // console.log(res, 111)
          // 获取token
          // this.$store.commit('user/SET_TOKEN', res.data)
          // 测试 获取用户信息
          // const userInfo = await getInfo()
          // console.log(userInfo)

          // this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            // this.loading = false
          })
          // .catch(() => {
          //   // this.loading = false
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe;
$cursor: #68b0fe;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 2.9375rem;
    width: 85%;

    input {
      background: transparent;
      border: 0rem;
      -webkit-appearance: none;
      border-radius: 0rem;
      padding: 0.75rem 0.3125rem 0.75rem 0.9375rem;
      color: #68b0fe;
      height: 2.9375rem;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0rem 62.5rem $bg inset;
        -webkit-text-fill-color: $cursor;
      }
    }
  }

  .el-form-item {
    border: 0.0625rem solid rgba(255, 255, 255, 0.1);
    background: #d5e5ff;
    border-radius: 0.3125rem;
    color: #454545;
  }
  // 设置错误信息的颜色
  .el-form-item__error {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/common/login.jpg') no-repeat center/cover;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 32.5rem;
    max-width: 100%;
    padding: 10rem 2.1875rem 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 0.875rem;
    color: #fff;
    margin-bottom: 0.625rem;

    span {
      &:first-of-type {
        margin-right: 1rem;
      }
    }
  }

  .svg-container {
    padding: 0.375rem 0.3125rem 0.375rem 0.9375rem;
    color: $dark_gray;
    vertical-align: middle;
    width: 1.875rem;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 1.625rem;
      color: $light_gray;
      margin: 0rem auto 2.5rem auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 0.625rem;
    top: 0.4375rem;
    font-size: 1rem;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.el-button--primary {
  height: 4rem;
  color: #fff;
  background-color: #409eff;
  border-color: #409eff;
  font-size: 1.5rem;
}
</style>
